<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.action.Soez9001ActionBean"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="com.feib.soeasy.model.User"%>

<script type="text/javascript">
	$(document).ready(function() {
		//$("#addResultDiv").dialog("destroy");
		$("#addResultDiv").dialog({
			title : '<%=UserUtil.getCurrentMenuItemName() %>',
			width : 600,
			resizable : false,
			modal : true
		});

		$("#cancelEditUserBtn").click(function() {
			$("#addResultDiv").dialog("close");
		});


		$("#doUnlockUserBtn").click(function() {
			// invoke ajax to save user.
			var pk = $('#editUser\\.userPk').val();
			$('#hidden\\.editUser\\.userPk').val(pk);
			$('#method1234').attr('name', 'doUnlockUser');
						
			waitingDialog({});
			$.ajax({
				url : "<c:url value='/ajax/Soez9001.action'/>",
				async : true,
				type : 'POST',
				data : 	$("#editUserHiddenForm").serialize(),
				dataType : 'html',
				success : function(data, textStatus, XMLHttpRequest) {
					$("#ajaxErrorMessageDiv").html(data);
					$("#ajaxErrorMessageDiv").dialog({
						title : '<%=UserUtil.getCurrentMenuItemName() %>',
						width : 300,
						resizable : false,
						modal : true,
						buttons : {
							Ok : function() {
								$(this).dialog("close");
								
								if (1==$("#doUnlockUserResultCode").val()){
									$("#addResultDiv").dialog("close");
									doQueryAction();
								}
							}
						}
					});
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
					$("#ajaxErrorMessageDiv").dialog({
						title : '系統錯誤',
						width : 600,
						resizable : false,
						modal : true,
						buttons : {
							Ok : function() {
								$(this).dialog("close");
							}
						}
					});
				},
				complete : function(XMLHttpRequest, textStatus) {
					closeWaitingDialog();
				}
			});
		});
		
		$("#doSaveUserBtn").click(function() {
			// invoke ajax to save user.
			var pk = $('#editUser\\.userPk').val();
			var userId = $('#editUser\\.userId').val();
			var userName = $('#editUser\\.userName').val();
			var phone = $('#editUser\\.userPhone').val();
			var email = $('#editUser\\.userEmail').val();
			var memo = $('#editUser\\.userMemo').val();
			var enabled = $('#editUser\\.enabled').val();
			
			var errMsg = "";
			if (null == userId || 0 == userId.length){
				errMsg = errMsg + "使用者ID 是必要欄位<br/>";
			}
			
			if (null == userName || 0 == userName.length){
				errMsg = errMsg + "姓名 是必要欄位<br/>";
			}
			
			if (null == email || 0 == email.length){
				errMsg = errMsg + "E-MAIL 是必要欄位<br/>";
			}
			else if (! verifyEmail(email)){
				errMsg = errMsg + "E-MAIL 格式錯誤<br/>";
			}
			
			if (0 != errMsg.length){
				$("#ajaxErrorMessageDiv").html(errMsg);
				$("#ajaxErrorMessageDiv" ).dialog({  
					title:'請修正下列錯誤' 
					,width: 300
					, resizable: false
					, modal: true
					, buttons: {
						Ok: function() {
							$( this ).dialog( "close" );
						}
					}
				});
				return false;
			}
			
			
			$('#hidden\\.editUser\\.userPk').val(pk);
			$('#hidden\\.editUser\\.userId').val(userId);
			$('#hidden\\.editUser\\.userName').val(userName);
			$('#hidden\\.editUser\\.userPhone').val(phone);
			$('#hidden\\.editUser\\.userEmail').val(email);
			$('#hidden\\.editUser\\.userMemo').val(memo);
			$('#hidden\\.editUser\\.enabled').val(enabled);
			
			doAjaxSaveUser();
		});
	});

	function doAjaxSaveUser() {
		$('#method1234').attr('name', 'doAjaxSaveUser');
		
		waitingDialog({});
		$.ajax({
			url : "<c:url value='/ajax/Soez9001.action'/>",
			async : true,
			type : 'POST',
			data : 	$("#editUserHiddenForm").serialize(),
			dataType : 'html',
			success : function(data, textStatus, XMLHttpRequest) {
				$("#ajaxErrorMessageDiv").html(data);
				$("#ajaxErrorMessageDiv").dialog("destroy");
				$("#ajaxErrorMessageDiv").dialog({
					title : '<%=UserUtil.getCurrentMenuItemName() %>',
					width : 300,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {
							$(this).dialog("close");
							
							if (1==$("#doAjaxSaveUserResultCode").val()){
								$("#addResultDiv").dialog("close");
								doQueryAction();
							}
						}
					}
				});
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
				$("#ajaxErrorMessageDiv").dialog("destroy");
				$("#ajaxErrorMessageDiv").dialog({
					title : '系統錯誤',
					width : 600,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {
							$(this).dialog("close");
						}
					}
				});
			},
			complete : function(XMLHttpRequest, textStatus) {
				closeWaitingDialog();
			}
		});

	}
</script>
<div id="editUserDiv">
<%
	// 當查詢到使用者不等於目前登入使用者群組代號，則要顯示業者代號欄位
	Soez9001ActionBean action = (Soez9001ActionBean)request.getAttribute("actionBean");
	User user = action.getEditUser();
	if (null != user && null != user.getGroup() && !UserUtil.getUserGroup().getGroupNo().equals(user.getGroup().getGroupNo()))
	    pageContext.setAttribute("textReadonly", true);
	else
	    pageContext.setAttribute("textReadonly", false);				    
%>
	<stripes:form id="editUserForm" name="editUserForm" beanclass="com.feib.soeasy.action.Soez9001ActionBean" method="post" onsubmit="javascript: return false;">
	
	<table id="queryForm">
		<tr>
			<td colspan="4" style="color: #990000">
				<strong>
					<c:choose>
						<c:when test="${null == actionBean.editUser.userPk }">使用者新增</c:when>
						<c:otherwise>使用者修改</c:otherwise>
					</c:choose> 
				</strong>
			</td>
			<stripes:hidden id="editUser.userPk" name="editUser.userPk" />
		</tr>
		<tr>
			<th>使用者ID<span id="mustKeyInStar">**</span></th>
			<td>
				<c:choose>
					<c:when test="${null != actionBean.editUser.userPk }"><stripes:text id="editUser.userId" name="editUser.userId" size="10" maxlength="10" readonly="true"/></c:when>
					<c:otherwise><stripes:text id="editUser.userId" name="editUser.userId" size="10" maxlength="10"/></c:otherwise>
				</c:choose>
				
			</td>
			<th>姓名<span id="mustKeyInStar">**</span></th>
			<td>
				<c:choose>
					<c:when test="${textReadonly }"><stripes:text id="editUser.userName" name="editUser.userName" size="20" maxlength="20" readonly="true"/></c:when>
					<c:otherwise><stripes:text id="editUser.userName" name="editUser.userName" size="20" maxlength="20"/></c:otherwise>
				</c:choose>
				
			</td>
		</tr>
		<tr>
			<th>電話</th>
			<td>
				<c:choose>
					<c:when test="${textReadonly }"><stripes:text id="editUser.userPhone" name="editUser.userPhone" size="10" maxlength="10" readonly="true"/></c:when>
					<c:otherwise><stripes:text id="editUser.userPhone" name="editUser.userPhone" size="10" maxlength="10"/></c:otherwise>
				</c:choose>
				
			</td>
			<th>E-MAIL<span id="mustKeyInStar">**</span></th>
			<td>
				<c:choose>
					<c:when test="${textReadonly }"><stripes:text id="editUser.userEmail" name="editUser.userEmail" size="30" maxlength="50" readonly="true"/></c:when>
					<c:otherwise><stripes:text id="editUser.userEmail" name="editUser.userEmail" size="30" maxlength="50"/></c:otherwise>
				</c:choose>
			</td>
		</tr>
		<tr>
			<th>啟用</th>
			<td>
				<c:choose>
					<c:when test="${textReadonly }">
						<stripes:select id="editUser.enabled_1" name="editUser.enabled" disabled="true">
							<stripes:option value="true">是</stripes:option>
							<stripes:option value="false">否</stripes:option>
						</stripes:select>
						<input type="hidden" id="editUser.enabled" name="editUser.enabled" value="<c:out value='${actionBean.editUser.enabled }'/>"/>
					</c:when>
					<c:otherwise>
						<stripes:select id="editUser.enabled" name="editUser.enabled">
							<stripes:option value="true">是</stripes:option>
							<stripes:option value="false">否</stripes:option>
						</stripes:select>
					</c:otherwise>
				</c:choose>
			</td>
			<th>被鎖</th>
			<td>
				<c:choose>
					<c:when test="${true == actionBean.editUser.locked }">是</c:when>
					<c:otherwise>否</c:otherwise>
				</c:choose>
				
			</td>
		</tr>
		<tr>
			<th>備註</th>
			<td colspan="3">
				<c:choose>
					<c:when test="${textReadonly }"><stripes:text id="editUser.userMemo" name="editUser.userMemo" size="50" maxlength="50" readonly="true"/></c:when>
					<c:otherwise><stripes:text id="editUser.userMemo" name="editUser.userMemo" size="50" maxlength="50"/></c:otherwise>
				</c:choose>
				
			</td>
		</tr>
		<tr>
			<th>登入狀態</th>
			<td>
				<strong>
					<c:choose>
						<c:when test="${true == actionBean.editUserLoginControl.actived }">登入</c:when>
						<c:otherwise>離線</c:otherwise>
					</c:choose> 
				</strong>
			</td>
			<th>上次登入時間</th>
			<td>
				<strong>
					<fmt:formatDate value="${actionBean.editUser.lastLogingDate}" pattern="yyyy/MM/dd HH:mm:ss"/>
				</strong>
			</td>
		</tr>
		<tr>
			<th>密碼有效時間</th>
			<td>
				<strong>
				<fmt:formatDate value="${actionBean.editUser.pwdExpiredDate }" pattern="yyyy/MM/dd HH:mm:ss" />				
				</strong>
			</td>
			<th></th>
			<td>
				
			</td>
		</tr>
		<tr>
			<td colspan="4" class="queryFunction">
				<c:if test="${null != actionBean.editUser.userPk && true == actionBean.editUser.locked }"><input id="doUnlockUserBtn" type="button" value="解鎖並初始密碼" /></c:if>
				<c:if test="${not textReadonly }"><input id="doSaveUserBtn" type="button" value="確認" /> </c:if>				
				<input type="button" value="離開" id="cancelEditUserBtn" />
			</td>
		</tr>
	</table>
</stripes:form></div>

<div id="editUserHiddenDiv" style="display: none;">
	<stripes:form id="editUserHiddenForm"	beanclass="com.feib.soeasy.action.Soez9001ActionBean" method="post" onsubmit="javascript: return false;">

		<input type="hidden" id="method1234" name="doAjaxSaveUser" />
		<input type="hidden" id="hidden.editUser.userPk" name="editUser.userPk" />
		<input type="hidden" id="hidden.editUser.userId" name="editUser.userId" />
		<input type="hidden" id="hidden.editUser.userName" name="editUser.userName" />
		<input type="hidden" id="hidden.editUser.userPhone"	name="editUser.userPhone" />
		<input type="hidden" id="hidden.editUser.userEmail"	name="editUser.userEmail" />
		<input type="hidden" id="hidden.editUser.userMemo"	name="editUser.userMemo" />
		<input type="hidden" id="hidden.editUser.enabled"	name="editUser.enabled" />
		
</stripes:form></div>

